<?php

namespace App\Http\Controllers\OpenShop\SiteLocation;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\AppletAuth;
use App\Http\Validate\OpenShop\SiteLocation\IntendedSiteSelectionValidate;
use App\Service\OpenShop\SiteLocation\AuthService;
use App\Service\OpenShop\SiteLocation\IntendedSiteSelectionService;
use App\Service\OpenShop\SiteLocation\SiteLocationService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class IntendedSiteSelectionController extends BaseController
{

    /**
     * 意向选址列表
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function list(Request $request): JsonResponse
    {
        return $this->success(IntendedSiteSelectionService::list(
            IntendedSiteSelectionValidate::listValidate($request->all()),
            $request->header("shopId"),
            request()->user()->id,
        ));
    }

    /**
     * 意向址添加
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function insert(Request $request): JsonResponse
    {
        IntendedSiteSelectionService::insert(
            IntendedSiteSelectionValidate::insertValidate($request->all()),
            $request->header("shopId"),
            request()->user()->id,
        );
        return $this->success();
    }

    /**
     * 意向址删除
     *
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public function del(Request $request): JsonResponse
    {
        IntendedSiteSelectionService::del(
            IntendedSiteSelectionValidate::delValidate(
                $request->all(),
                $request->header("shopId"),
                request()->user()->id,
            ),
            request()->user()->id,
        );
        return $this->success();
    }

    public function detail(Request $request, $id)
    {
        return $this->success(IntendedSiteSelectionService::detail($id));
    }

    public function verifySiteSelection(Request $request)
    {

    }


    /**
     * 通过经纬度获得点位信息
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     * @throws GuzzleException
     */
    public function dotInfoByLatAndLng(Request $request): JsonResponse
    {
        $this->validate($request, [
            'lng' => 'required',
            'lat' => 'required',
        ]);
        $lng = $request->get('lng', 0);
        $lat = $request->get('lat', 0);
        return $this->success(IntendedSiteSelectionService::dotInfoByLatAndLng($lng, $lat));
    }


    /**
     * 通过经纬度获得网规信息
     * @throws ValidationException
     */
    public function wangguiInfoByLatAndLng(Request $request): JsonResponse
    {
        $this->validate($request, [
            'lng'  => 'required',
            'lat'  => 'required',
            //'type' => 'required',
        ]);
        $lng    = $request->get('lng', 0);
        $lat    = $request->get('lat', 0);
        $type   = (string)$request->get('type', 'kfShop');
        $radius = (int)$request->get('radius', 3000);
        $rmsUserId = request()->user()->id;
        return $this->success(IntendedSiteSelectionService::wangguiInfoByLatAndLng($lng, $lat, $type, $radius, $rmsUserId));
    }

    /**
     * 获得分公司列表
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function subCompany(): JsonResponse
    {
        return $this->success(['list' => IntendedSiteSelectionService::subCompany()]);
    }

    /**
     * 通过经纬度获得点位信息
     * @param Request $request
     * @return JsonResponse
     */
    public function intendedCity(Request $request): JsonResponse
    {
        $shopId = (int)$request->header('shopId');
        $lng    = $request->get('lng');
        $lat    = $request->get('lat');
        return $this->success(IntendedSiteSelectionService::intendedCity($shopId, $lat, $lng));
    }

    /**
     * @throws ValidationException
     */
    public function pointInfoByLatAndLng(Request $request): JsonResponse
    {
        $lng         = $request->get('lng', 0);
        $lat         = $request->get('lat', 0);
        $type        = (string)$request->get('type', '');
        $radius      = (int)$request->get('radius', 3000);
        $searchInput = (string)$request->get('searchInput', '');
        $cityCode    = (int)$request->get('cityCode', 0);
        $rmsUserId   = request()->user()->id;

        $id          = (int)$request->get('id', 0);

        return $this->success(IntendedSiteSelectionService::pointInfoByLatAndLng($lng, $lat, $type, $radius, $searchInput, $cityCode, $rmsUserId, $id));
    }

    /**
     * @throws ValidationException
     */
    public function collection(Request $request): JsonResponse
    {
        $this->validate($request, [
            'collection' => 'required|int',
            'wgNumber'   => 'required|string',
            'type'       => 'required|string',
        ]);
        $collection = (int)$request->post('collection', 0);
        $wgNumber   = (string)$request->post('wgNumber', '');
        $type       = (string)$request->post('type', '');
        $rmsUserId  = request()->user()->id;

        IntendedSiteSelectionService::collection($collection, $wgNumber, $type, $rmsUserId);

        return $this->success();
    }

    /**
     * @throws ValidationException
     */
    public function assignManager(Request $request, $id)
    {
        $this->validate($request, [
            'managerId' => 'required|int',
        ]);

        $rmsUserName = request()->user()->username;

        $managerId = $request->post('managerId', 0);

        if ($managerId) {
            IntendedSiteSelectionService::assignManager($id, $managerId, $rmsUserName);
        }

        return $this->success();
    }

    /**
     * @throws ValidationException
     */
    public function submitRecord(Request $request, $id): JsonResponse
    {
        $this->validate($request, [
            'status' => 'required|int',
            'files'  => 'array',
            'record' => 'required|string',
        ]);

        $status      = (int)$request->post('status', 0);
        $files       = (array)$request->post('files', []);
        $record      = (string)$request->post('record', '');
        $authUser    = AuthService::getRmsUserInfo($request);
        $rmsUserId   = $authUser['userId'] ?? 0;
        $rmsUsername = $authUser["username"] ?? "";

        IntendedSiteSelectionService::submitRecord($id, $status, $files, $record, $rmsUserId, $rmsUsername);

        return $this->success();
    }

    public function record(Request $request): JsonResponse
    {
        $cityType    = (int)$request->get('cityType', 0);
        $cityCode    = (int)$request->get('cityCode', 0);
        $type        = (string)$request->get('type', '');
        $status      = (string)$request->get('status', '');
        $managerId   = (string)$request->get('managerId', 0);
        $searchInput = (string)$request->get('searchInput', '');
        $pageNum     = (int)$request->get('pageNum', 1);
        $pageSize    = (int)$request->get('pageSize', 10);
        $authUser    = AuthService::getRmsUserInfo($request);
        $rmsUserId   = $authUser['userId'] ?? 0;

        return $this->success(IntendedSiteSelectionService::recordList($cityType, $cityCode, $type, $status, $managerId, $searchInput, $pageNum, $pageSize, $rmsUserId));
    }
}
